<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-5-31
 * Time: 下午11:07
 */

class money extends adminBase{
    /** @var  MoneyMode */
    protected $moneyMode;
    protected $moneyModeChild;
    function __construct(){
        parent::__construct();
        $this->cms->setPageTitle("财务管理");
        $this->cms->setControlFile("admin/tpl/money/money.json");
        $this->moneyMode=MoneyMode::init();
        $this->moneyModeChild=MoneyModeChild::init();
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(13,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page,$event_types,$username,$start_time,$end_time){
        $r['type']=$event_types;
        $r['username']=$username;
        $r['start_time']=$start_time;
        $r['end_time']=$end_time;
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $service=ServiceMode::init();
        $shop=ShopMode::init();
        $user=UserModule::init('user');
        if(isset($username)){
            $user_id_exist=$user->getIdByUsername($username);
            $user_id=isset($user_id_exist)?$user_id_exist:-1;
        }else{
            $user_id=null;
        }
        if((isset($event_types)&&($event_types!='applyMoney'))||empty($event_types)) {
            list($r['money_info'],$money_total)=$this->moneyModeChild->getList($user_id,$event_types,$countOfPage,$offset,$start_time,$end_time);
            $table_url="admin/tpl/money/money_info.php";
        }else {
            list($r['money_info'],$money_total)=$this->moneyModeChild->getCashList($countOfPage,$offset,$start_time,$end_time,$user_id);
            $table_url="admin/tpl/money/money.php";
        }
        $r['money_info']=is_array($r['money_info'])?$r['money_info']:array();
        foreach($r['money_info'] as $v){
            $serviceId=$service->getServiceId($v['user_id']);
            $service_info=$service->getDetail($serviceId);
            $r[$v['user_id']]['service_name']=isset($service_info)?$service_info['service_name']:null;
            $shopId=$shop->getShopId($v['user_id']);
            $shop_info=$shop->getDetail($shopId);
            $r[$v['user_id']]['shop_name']=isset($shop_info)?$shop_info['company']:null;
            $user_info=$user->getDetail($v['user_id']);
            $r[$v['user_id']]['user_id']=$user_info['username'];
        }
        $r['event_types']=array("applyMoney"=>"提现申请信息","serviceFee"=>"服务费信息","shopSell"=>"商家货款信息"," "=>"用户支付信息","buy"=>"代购提成信息","share"=>"分享提成信息","platsystem"=>"普团网平台费");
        $r['event_type']=array("applyMoney"=>"提现申请","serviceFee"=>"服务费","shopSell"=>"商家货款"," "=>"用户支付","buy"=>"代购提成","share"=>"分享提成");
        $r['pay_type']=array("money"=>"现金","alipay"=>"支付宝");
        $url=WebRouter::init()->getQuestion("username={$username}&event_types={$event_types}&start_time={$start_time}&end_time={$end_time}&page=");
        $r['pages']=new Page($page,$money_total,$countOfPage);
        $r['pages']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
            $this->cms->tableScene($r,$table_url);

    }

    function confirmTask($id){
        if($this->moneyMode->authCash($id,1)){
            $r['url']=WebRouter::init()->getPage("money","index");
            $r['message']="确认成功";
        }else{
            $r['message']="确认失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function cancelConfirmTask($id){
        if($this->moneyMode->authCash($id,0)){
            $r['url']=WebRouter::init()->getPage("money","index");
            $r['message']="撤销确认成功";
        }else{
            $r['message']="撤销确认失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function checkTask($page){
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $member=UserModule::init('user');
        list($r['member'],$member_total)=$member->getmoneyuserList($countOfPage,$offset,0);    
        $r['pages']=new Page($page,$member_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $r['userType']=array("shop"=>"商家用户","user"=>"普通用户","service"=>"服务站用户","both"=>"服务站/商家用户");
        $this->cms->tableScene($r,"admin/tpl/money/money_check.php");
    }

    function checkSubmitTask($username){
        $username=isset($username)?trim($username):null;
        $user_id= UserMode::init()->getIdByUsername($username);
        $id=UserMode::init()->getIdByUserId($user_id);
        if(UserMode::init()->setMoneyUsable($id,0)){
            $r['url']=WebRouter::init()->getPage("money","index");
            $r['message']="冻结账户成功";
        }else{
            $r['message']="冻结账户失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function unfreezeTask($page){
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $member=UserModule::init('user');
        list($r['member'],$member_total)=$member->getmoneyuserList($countOfPage,$offset,1);    
        $r['pages']=new Page($page,$member_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $r['userType']=array("shop"=>"商家用户","user"=>"普通用户","service"=>"服务站用户","both"=>"服务站/商家用户");
        $this->cms->tableScene($r,"admin/tpl/money/money_unfreeze.php");
//        $this->cms->formScene(array(),"admin/tpl/money/money_unfreeze.php");
    }

    function unfreezeSubmitTask($username){
        $username=isset($username)?trim($username):null;
        $user_id= UserMode::init()->getIdByUsername($username);
        $id=UserMode::init()->getIdByUserId($user_id);
        if(UserMode::init()->setMoneyUsable($id,1)){
            $r['url']=WebRouter::init()->getPage("money","index");
            $r['message']="解冻账户成功";
        }else{
            $r['message']="解冻账户失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function changeTask(){
        $this->cms->formScene(array(),"admin/tpl/money/money_change.php");
    }

    function changeSubmitTask($username,$money,$asc,$remark){
        $username=isset($username)?trim($username):null;
        $remark = isset($remark)?trim($remark):'更改账号余额';
        $user_id = UserMode::init()->getIdByUsername($username);
        if($asc=='up'){
            if($this->moneyMode->updateMoney($user_id,$money,true)){
                $userInfo=UserMode::init()->getDetailWithoutJoin($user_id);
                $addHistory = $this->moneyMode->add($user_id,'更改账号余额',+$money,$userInfo['money'],$remark,null,'',null,1,null);
            }else{
                $addHistory = -1;
            }
        }else{
            if($this->moneyMode->updateMoney($user_id,$money,false)){
                $userInfo=UserMode::init()->getDetailWithoutJoin($user_id);
                $addHistory = $this->moneyMode->add($user_id,'更改账号余额',-$money,$userInfo['money'],$remark,null,'',null,1,null);
            }else{
                $addHistory = -1;
            }
        }
        if($addHistory)
        {
            $r['url']=WebRouter::init()->getPage("money","index");
            $r['message']="更改成功";
        }else{
            $r['message']="更新失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
    
    function downloadTask($event_types,$username,$start_time,$end_time){
        require_once dirname(dirname(__FILE__)).'/tool/ExcelWrite.class.php'; 
        $countOfPage=20;
        $offset=10;
        $r['type']=$event_types;
        $r['username']=$username;
        $r['start_time']=$start_time;
        $r['end_time']=$end_time;
        $service=ServiceMode::init();
        $shop=ShopMode::init();
        $user=UserModule::init('user');
        if(isset($username)){
            $user_id_exist=$user->getIdByUsername($username);
            $user_id=isset($user_id_exist)?$user_id_exist:-1;
        }else{
            $user_id=null;
        }
        if((isset($event_types)&&($event_types!='applyMoney'))||empty($event_types)) {
            list($r['money_info'],$money_total)=$this->moneyModeChild->getList($user_id,$event_types,$countOfPage,$offset,$start_time,$end_time,1);
            $table_url="admin/tpl/money/money_info.php";
        }else {
            if($user_id){
                list($r['money_info'],$money_total)=$this->moneyModeChild->getCashListByUserId($user_id,$countOfPage,$offset,$start_time,$end_time);
            }else{
                list($r['money_info'],$money_total)=$this->moneyModeChild->getCashList($countOfPage,$offset,$start_time,$end_time);
            }
            $table_url="admin/tpl/money/money.php";
        }
        $r['money_info']=is_array($r['money_info'])?$r['money_info']:array();
  
        $array=$r['money_info'];
        $event_types=array("applyMoney"=>"提现申请信息","serviceFee"=>"服务费信息","shopSell"=>"商家货款信息"," "=>"用户支付信息","buy"=>"代购提成信息","share"=>"分享提成信息","ptf"=>"普团网平台费");
        foreach($array as $k=>$v){
            $serviceId=$service->getServiceId($v['user_id']);
            $service_info=$service->getDetail($serviceId);
            $array[$k]['service_name']=isset($service_info)?$service_info['service_name']:null;
            $shopId=$shop->getShopId($v['user_id']);
            $shop_info=$shop->getDetail($shopId);
            $array[$k]['shop_name']=isset($shop_info)?$shop_info['company']:null;
            $user_info=$user->getDetail($v['user_id']);
            if($user_info){
              $array[$k]['user_id']=$user_info['username'];   
            }
            foreach($event_types as $m=>$s){
                if($v['event_type']==$m) 
                   $array[$k]['event_type_name']=  $s;
            }
        }
        
        $b=array();
        $b[0]=array('ID','用户名','事件','金额','余额','备注','交易流水号','创建时间','事件类型','付款方式','交易是否成功','订单ID','服务站名称','商家名称 ','事件名称');
        foreach($array as $k=>$v){
            $m=$k+1;
            $b[$m]=$v;
        }
//        echo "<pre>";
//        var_dump($b);
//        echo "</pre>";
        $excel=new ExcelWrite();
        $filename="test".date("Y_m_d H_i_s",time()).".xls";
        $excel->downloadFile($b, $filename);

    }
} 